{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# White-box Targeted Attack on CIFAR10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load model and data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Files already downloaded and verified\n",
      "[Data loaded]\n",
      "[Model loaded]\n",
      "Acc: 100.00 %\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from robustbench.data import load_cifar10\n",
    "from robustbench.utils import load_model, clean_accuracy\n",
    "\n",
    "images, labels = load_cifar10(n_examples=5)\n",
    "print('[Data loaded]')\n",
    "\n",
    "device = 'cuda'\n",
    "model = load_model('Standard', norm='Linf').to(device)\n",
    "acc = clean_accuracy(model, images.to(device), labels.to(device))\n",
    "print('[Model loaded]')\n",
    "print('Acc: %2.2f %%'%(acc*100))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adversarial Attack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Attack mode is changed to 'targeted(label)'.\n",
      "PGD(model_name=WideResNet, device=cuda:0, attack_mode=targeted(label), targeted=True, normalization_used=False, eps=0.03137254901960784, alpha=0.008888888888888889, steps=10, random_start=True)\n"
     ]
    }
   ],
   "source": [
    "from torchattacks import PGD\n",
    "atk = PGD(model, eps=8/255, alpha=2/225, steps=10, random_start=True)\n",
    "atk.set_mode_targeted_by_label()\n",
    "print(atk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([3, 8, 8, 0, 6])\n",
      "tensor([4, 9, 9, 1, 7])\n"
     ]
    }
   ],
   "source": [
    "print(labels)\n",
    "new_labels = (labels + 1) % 10\n",
    "print(new_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "targeted(label)\n",
      "tensor([3, 8, 8, 0, 6])\n",
      "tensor([4, 9, 9, 1, 7], device='cuda:0')\n"
     ]
    }
   ],
   "source": [
    "adv_images = atk(images, new_labels)\n",
    "adv_pred = model(adv_images)\n",
    "print(labels)\n",
    "print(torch.argmax(adv_pred, 1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
